package com.cloudeye.business.base.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudeye.business.base.util.DateUtil;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lafangyuan on 2021/6/28.
 */
@Component
public class LugPsgToMq {

    @Autowired
    private DbService dbService;

    public JSONArray create_CZ6938_LugJson(Map<String,Object> psg) throws IOException, InterruptedException {
        URL url = ResourceUtils.getURL("classpath:data/lug.json");
        UrlResource resource = new UrlResource(url);
        EncodedResource enResource = new EncodedResource(resource,"utf-8");
        String result = FileCopyUtils.copyToString(enResource.getReader());
        JSONArray jsonArray = JSON.parseArray(result);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            obj.put("primaryValue",randomKey(8));
            obj.put("lugBoardno",psg.get("psg_boardno"));
            obj.put("lugDest",psg.get("psg_dest"));
            Date fltDate = (Date) psg.get("psg_fltdt");
            obj.put("lugFltdt",DateUtil.parseDateToString(fltDate,"yyyy-MM-dd"));

            obj.put("lugFltno",psg.get("psg_fltno"));
            obj.put("lugOrigin",psg.get("psg_origin"));


            obj.put("lugNameCh",psg.get("psg_name_cn"));
            obj.put("lugNameEn",psg.get("psg_name_en"));
            obj.put("lugSeatno",psg.get("psg_seatno"));
            obj.put("lugNo",randomLugNo());
            obj.put("lugId",randomLugId());
            obj.put("lugRecordTime",System.currentTimeMillis());
            obj.put("lugTime",System.currentTimeMillis());
        }
        return jsonArray;
    }

    private byte randomChar() {
        int flag = (int) (Math.random() * 2);// 0小写字母1大写字母
        byte resultBt;
        if (flag == 0) {
            byte bt = (byte) (Math.random() * 26);// 0 <= bt < 26
            resultBt = (byte) (65 + bt);
        } else {
            byte bt = (byte) (Math.random() * 26);// 0 <= bt < 26
            resultBt = (byte) (97 + bt);
        }
        return resultBt;
    }
    public JSONArray create_CZ6938_PsgJson(Map<String,Object> flight) throws IOException, InterruptedException {
        URL url = ResourceUtils.getURL("classpath:data/psg.json");
        UrlResource resource = new UrlResource(url);
        EncodedResource enResource = new EncodedResource(resource,"utf-8");
        String result = FileCopyUtils.copyToString(enResource.getReader());
        JSONArray jsonArray = JSON.parseArray(result);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            obj.put("primaryValue",randomKey(8));
            obj.put("psgBoardno",randomKey(3));
            obj.put("psgCheckinCounter",randomKey(6));
            obj.put("psgFltdt",DateUtil.parseDateToString(new Date(),"yyyy-MM-dd"));
            obj.put("psgId",randomKey(8));
            obj.put("psgIdcardNo",randomKey(12));
            obj.put("psgNameCh",randomName());
            obj.put("psgNameEn",randomNameEn());
            obj.put("psgRecordTime",System.currentTimeMillis());
            obj.put("psgSeatno",randomKey(3));
        }
        return jsonArray;
    }


    //获得单个汉字
    public String getChinese(){
        String chinese = "";
        int i = (int)(Math.random()*40 + 16);
        int j = (int)(Math.random()*94 + 1);
        if(i == 55){
            j = (int)(Math.random()*89 + 1);
        }
        byte[] bytes = {(byte) (i+160),(byte) (j+160)};
        try {
            chinese =  new String(bytes, "gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return chinese;
    }
    private Object randomNameEn() {
        int length = 12 + (int) (Math.random() * 9);
        String word = "";
        for (int i = 0; i < length; i++) {
            word += (char) randomChar();
        }
        return word;
    }

    /**
     * 替换
     * FLID 11731663:随机
     * DDTM 20210621012624  ,
     * FFID TV-6013-20210621-A
     * FEXD 20210621
     * @return
     * @throws IOException
     */
    public String createFlightXml() throws IOException, InterruptedException {
       //默认是21号的数据
        String execDateOld = "20210621";
        URL url = ResourceUtils.getURL("classpath:data/flight.xml");
        UrlResource resource = new UrlResource(url);
        EncodedResource enResource = new EncodedResource(resource,"utf-8");
        String result = FileCopyUtils.copyToString(enResource.getReader());
        //DDTM ,FFID,FEXD
        String execDate = DateUtil.parseDateToString(new Date(),"yyyyMMdd");
        // 20210621->20210629,20210620->20210628
        result = result.replace(execDateOld,execDate);
        //FLID
        Pattern patternFLID = Pattern.compile("<FLID>\\d*</FLID>");
        Matcher mFLID = patternFLID.matcher(result);
        while (mFLID.find()){
            String group = mFLID.group();
            String afid = group.replace("<FLID>","").replace("</FLID>","");
            result = result.replace(afid,randomFlid());
        }
        //        result = mFLID.replaceAll(randomFlid());
        System.out.println(result);
        return result;
    }
    public String randomFlid() throws InterruptedException {
        Thread.sleep(1000);
        String k =  String.valueOf(System.currentTimeMillis());
        return k.substring(k.length()-9,k.length()-1);
    }


    public String randomLugNo() throws InterruptedException {
        Thread.sleep(1000);
        String k =  String.valueOf(System.currentTimeMillis());
        return k.substring(k.length()-11,k.length()-1);
    }
    public String randomLugId() throws InterruptedException {
        Thread.sleep(1000);
        String k =  dbService.getNextLugId().toString();
        return k;
    }
    public String randomKey(int size) throws InterruptedException {
        Thread.sleep(1000);
        size=size+1;
        String k =  String.valueOf(System.currentTimeMillis());
        return k.substring(k.length()-size,k.length()-1);
    }
    public String randomName(){
        String name = "";
        int chineseNameNum = (int)(Math.random()*3 + 2);
        for(int i=1; i<=chineseNameNum;i++){
            name += this.getChinese();
        }
        return name;
    }
    public static void main(String[] args) throws InterruptedException {
        System.out.println(new LugPsgToMq().randomLugId().length());
    }
}
